package cn.aiyuan;

import lombok.val;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.api.common.typeinfo.TypeHint;
import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;

import java.util.Properties;

/**
 * @author wangyuan
 * @date 2021/7/8
 */
public class KafkaWordCount {

    public static void main(String[] args) throws Exception {
        final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();


        //kafka配置文件
        Properties properties = new Properties();
        properties.setProperty("bootstrap.servers", "kafka-1.xha.com:9092");
        properties.setProperty("group.id", "aiyuan");
        properties.setProperty("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        properties.setProperty("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        properties.setProperty("auto.offset.reset", "latest");

        DataStreamSource<String> dataStreamSource = env.addSource(new FlinkKafkaConsumer<>("aiyuan_flink_msg", new SimpleStringSchema(), properties));
        SingleOutputStreamOperator<Tuple2<String, Integer>> streamOperator = dataStreamSource.flatMap((FlatMapFunction<String, Tuple2<String, Integer>>) (value, collector) -> {
            String[] split = value.split(" ");
            for (String word : split) {
                collector.collect(new Tuple2<>(word, 1));
            }
        }).returns(TypeInformation.of(new TypeHint<Tuple2<String, Integer>>(){}))
                //.keyBy(0)
                .keyBy((KeySelector<Tuple2<String, Integer>, String>) tuple2 -> tuple2.getField(0))
                .window(TumblingProcessingTimeWindows.of(Time.seconds(15)))
                .sum(1);

        streamOperator.print();
        env.execute("test aiyuan");
    }
}
